<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!--url是请求数据的地址-->
<table class="easyui-datagrid" id="notice-datagrid">
</table>
<div id="notice-datagrid-toolbar">
    <div>
        ID：<input type="text" class="easyui-numberbox" id="search-notice-id"/>
        <a href="#" class="easyui-linkbutton" iconCls="icon-search"
           onclick="doNoticeSearch()">搜索</a>
    </div>
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="doEditNotice()">新增</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="doDeleteNotice()">删除</a>
</div>
<div class="easyui-dialog" href="<%=request.getContextPath()%>/notice_edit.jsp" closed="true" id="add-notice-dialog"
     modal="true" title="编辑公告" style="width:720px;height:500px;padding-top:30px" buttons="#dlg-notice-buttons">
</div>
<div id="dlg-notice-buttons">
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="submitNotice()">保存</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#add-notice-form').form('reset');">重填</a>
</div>
<link href="<%=request.getContextPath()%>/media/kindeditor-4.1.10/themes/default/default.css" type="text/css"
      rel="stylesheet">
<script type="text/javascript" charset="utf-8"
        src="<%=request.getContextPath()%>/media/kindeditor-4.1.10/kindeditor-all.js"></script>
<script type="text/javascript" charset="utf-8"
        src="<%=request.getContextPath()%>/media/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script type="text/javascript">
    $(function () {
        $("#notice-datagrid").datagrid({
            url: '/notice.html?act=list',
            singleSelect: true,
            pagination: true,
            nowrap: false,
            toolbar: "#notice-datagrid-toolbar",
            columns: [[
                {field: 'id', title: 'ID', width: 80},
                {field: 'title', title: '标题', width: 180},
                {field: 'createdDate', title: '创建时间', width: 160},
                {
                    field: 'status', title: '状态', width: 80, formatter: function (val, row) {
                    switch (row.status) {
                        case 0:
                            return "<span style='color:#ff0200'>无效</span>";
                        case 1:
                            return "<span style='color:#00cf4d'>有效</span>";
                    }
                }
                }
            ]]
        })
    })
    var noticeEditorParams = {
        filePostName: "file",//指定上传文件参数名称
        uploadJson: '<%=request.getContextPath()%>/notice.html?act=upload',//指定上传文件请求的url。
        dir: "image"//上传类型，分别为image、flash、media、file,
    }
    var noticeEditor;
    function doEditNotice() {
        $("#add-notice-dialog").dialog({
            onLoad: function () {
                noticeEditor = KindEditor.create($("#notice-editor"), noticeEditorParams);
            },
            onBeforeDestroy: function () {
                KindEditor.remove("#notice-editor");
            }
        }).dialog('open');
    }

    function doNoticeSearch() {
        var queryParams = $('#notice-datagrid').datagrid('options').queryParams;
        queryParams.id = $("#search-notice-id").val();
        //重新加载datagrid
        $("#notice-datagrid").datagrid('reload');
    }

    function submitNotice() {
        $('#add-notice-form').form('submit', {
            onSubmit: function () {
                if ($(this).form('enableValidation').form('validate')) {
                    $.ajax({
                        url: "<%=request.getContextPath()%>/notice.html?act=add",
                        data: $("#add-notice-form").serialize(),
                        method: "post",
                        success: function (data) {
                            if (data.status) {
                                $("#notice-datagrid").datagrid('reload');
                                $("#add-notice-dialog").dialog("close");
                                $('#add-notice-form').form('clear');
                            } else {
                                alert(data.message)
                            }
                        }
                    })
                }
                return false;
            }
        });
    }

    function doDeleteNotice() {
        var row = $('#notice-datagrid').datagrid('getSelected');
        if (row == null) {
            return;
        }
        if (confirm("确定要删除吗？")) {
            $.ajax({
                url: "<%=request.getContextPath()%>/notice.html?act=delete",
                data: "id=" + row.id,
                method: "post",
                success: function (data) {
                    if (data.status) {
                        $("#notice-datagrid").datagrid('reload');
                    } else {
                        alert(data.message)
                    }
                }
            })
        }
    }
</script>